package com.ruoyi.reception;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.receipt.domain.TbUser;
import com.ruoyi.receipt.utils.CurrentUserUtils;
import com.ruoyi.receipt.utils.FormulaUtil;
import com.ruoyi.receipt.utils.StatisAnalysisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 推入切出次数 切出时长 学习时长 获取综合分控制器
 */
@RestController
@RequestMapping("/reception/pushIntoGetScoreRec")
public class PushIntoGetScoreRecController extends BaseController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StatisAnalysisUtils statisAnalysisUtils;

    @Autowired
    FormulaUtil formulaUtil;

    //根据登录用户获取综合分 时间分 创新分 能力分 专注分
    @RequestMapping("/getComprehensiveScore")
    @ResponseBody
    public AjaxResult getComprehensiveScore(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String formatDate = simpleDateFormat.format(date);
        //获取手机端当前登录用户
        TbUser currentUser = CurrentUserUtils.getCurrentUser();
        String loginAccount = currentUser.getId().toString();
        //1.统计今日总做题数
        Long todyCount = redisTemplate.opsForZSet().size(RedisConstants.TODAY_ALL_SUBJECT + formatDate+":"+loginAccount);
        //2.统计今日错题数
        Long todyWrongCount = redisTemplate.opsForZSet().size(RedisConstants.TODAY_WRONG_SUBJECT + formatDate+":"+loginAccount);
        //3.统计今日切出次数
        Double todyCutOutTimes = redisTemplate.opsForZSet().score(RedisConstants.TODAY_CUT_OUT_NUMBER+formatDate, Long.parseLong(loginAccount));
        if (todyCutOutTimes == null){
            todyCutOutTimes = 0.0;
        }
        //4.统计今日切出时长
        Double cutOutTime = redisTemplate.opsForZSet().score(RedisConstants.TODAY_CUT_OUT_TIME+formatDate, Long.parseLong(loginAccount));
        if (cutOutTime == null){
            cutOutTime = 0.0;
        }
        //5.统计今日正确率(存放int类型整数 前端回显拼上百分号)
        Double round = Double.valueOf(0);
        if (todyCount.intValue() > 0){
            //判断今日是否做过题  没做过题的话正确率返回0
            round = Math.floor(((todyCount.doubleValue() - todyWrongCount.doubleValue()) / todyCount.doubleValue()) * 100);
        }
        //6.统计今日学习时长
        Double learnTime = redisTemplate.opsForZSet().score(RedisConstants.TODAY_LEARN_TIME+formatDate, Long.parseLong(loginAccount));
        if (learnTime == null){
            learnTime = 0.0;
        }
        //7.统计今日难题正确率
        Long difGre35AllNum1 = statisAnalysisUtils.getDifGre35AllNum(Long.parseLong(loginAccount), formatDate);//用户做过的难度大于35的所有题目个数
        Long difGre35RightNum = statisAnalysisUtils.getDifGre35RightNum(Long.parseLong(loginAccount), formatDate);//用户做过的难度大于35的正确题目个数
        Double accuracyOfDifficulty = Double.valueOf(0);
        //判断用户是否做过的难度大于35
        if (difGre35AllNum1.intValue() > 0){
            accuracyOfDifficulty = Math.floor((difGre35RightNum.doubleValue() / difGre35AllNum1.doubleValue()) * 100);
        }
        //查询综合分
        int synthesizeScore = formulaUtil.synthesize(learnTime.intValue(), round.intValue(), accuracyOfDifficulty.intValue(), todyCutOutTimes.intValue(), cutOutTime.intValue());
        String score = String.valueOf(synthesizeScore);
        //查询时间分 传入今日学习时长
        int timeScore = formulaUtil.time(learnTime.intValue());
        //查询创新分 传入难题正确率
        int innovateScore = formulaUtil.innovate(accuracyOfDifficulty.intValue());
        //查询能力分 传入正确率
        int capabilitiesScore = formulaUtil.capacity(round.intValue());
        //查询专注分 传入切出次数 切出分钟
        int focusScore = formulaUtil.fengrossment(todyCutOutTimes.intValue(), cutOutTime.intValue());
        Map resultMap = new HashMap();
        //放入综合分
        resultMap.put("synthesizeScore",score);
        //放入时间分
        resultMap.put("timeScore",timeScore);
        //放入创新分
        resultMap.put("innovateScore",innovateScore);
        //放入能力分
        resultMap.put("capabilitiesScore",capabilitiesScore);
        //放入专注分
        resultMap.put("capacityScore",focusScore);
        return AjaxResult.success(resultMap);
    }

    /**
     * 推入切出次数
     */
    @RequestMapping("/pushCutOutTimes")
    public AjaxResult pushCutOutTimes(){
        TbUser currentUser = CurrentUserUtils.getCurrentUser();
        String loginAccount = currentUser.getId().toString();
        statisAnalysisUtils.putTodayCutOutNumber(Long.parseLong(loginAccount));
        return AjaxResult.success();
    }

    /**
     * 推入切出时长
     */
    @PostMapping("/pushCutOutTime")
    public AjaxResult pushCutOutTime(@RequestBody Map map){
        String time = (String)map.get("time");
        TbUser currentUser = CurrentUserUtils.getCurrentUser();
        String loginAccount = currentUser.getId().toString();
        statisAnalysisUtils.putTodayCutOutTime(Long.parseLong(loginAccount),Integer.parseInt(time));
        return AjaxResult.success();
    }

    /**
     * 推入学习时长
     */
    @PostMapping("/pushLearnTime")
    public AjaxResult pushLearnTime(@RequestBody Map map){
        String time = (String)map.get("time");
        TbUser currentUser = CurrentUserUtils.getCurrentUser();
        String loginAccount = currentUser.getId().toString();
        statisAnalysisUtils.putTodayLearnTime(Long.parseLong(loginAccount),Integer.parseInt(time));
        return AjaxResult.success();
    }

}
